package cn.lxh.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.lxh.result.JSONResult;
import cn.lxh.template.OssTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author lxh Create Time: 2023/12/29 17:32
 * 阿里OSS文件上传公共接口
 */
@RestController
@RequestMapping("/app/file")
public class AppFileController {

    @Autowired
    private OssTemplate ossTemplate;

    @Operation( summary= "oss文件上传",description = "oss文件上传")
    @Parameter(name = "file",description = "MultipartFile对象",required = true)
    @PostMapping("/oss/upload")
    @SaIgnore
    public JSONResult uploadOSS(@RequestPart("file") MultipartFile file) {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 将MultipartFile类型转为File类型 (如果不转类型, 可以获取输入流, 参数就要传输入流, oss文件上传参数就从file变为输入流也是可以的)
        File uploadFile = ossTemplate.transferToFile(file);
        // 上传文件
        String ossPath = ossTemplate.upload(fileName, uploadFile);
        return JSONResult.success(ossPath);
    }
}
